package com.kwlt.smc.service;


import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.kwlt.smc.dao.KeywordDao;
import com.kwlt.smc.dao.UserChannelDao;
import com.kwlt.smc.entity.Keyword;
import com.kwlt.smc.entity.SmsInfo;
import com.kwlt.smc.entity.User;
import com.kwlt.smc.util.CacheMap;
import com.kwlt.smc.web.SendSmsController;


/**
 * 用户管理类
 * @author xushunxin
 *
 */
//Spring Bean的标识.
@Component
//默认将类中的所有public函数纳入事务管理.
@Transactional(readOnly = true)
public class KeywordService {
	private static Logger logger = LoggerFactory.getLogger(SendSmsController.class);
	//全局关键字
	List<Keyword> globalKeyword=null;
	//通道关键字
	Map<String,List<Keyword>> channelMapKeyword=null;
	//用户关键字
	Map<String,List<Keyword>> userMapKeyword=null;
	//运营商关键字
	Map<String,List<Keyword>> operatorsMapKeyword=null;
	@Autowired
	private UserChannelDao userChannelDao;
	@Autowired
	private KeywordDao keywordDao;
	
	/**
	 * 
	 * @param smsInfo 用户参数
	 * @param type 1 globalKeyword 2 operatorsMapKeyword 3 channelMapKeyword 4 userMapKeyword
	 * //type=2 operators type=3 channelCode
	 * @param operators 验证运营商关键字，运营商编号 mobile,unicom,telecom
	 * @param channelCode 运营上编号 sh000,sh001..
	 * @return
	 */
	//
	public boolean validationKeyword(SmsInfo smsInfo,String type,String operators,String channelCode) {
		//如果短信内容为空
		
		if( StringUtils.isEmpty(smsInfo.getMsg())){
			return false;
		}
		// 如果不需要过滤
		String isFilterKey=smsInfo.getOuser().getIsFilterKey();
		if (StringUtils.isEmpty(isFilterKey) || "1".equals(isFilterKey)) {
			return true;
		} else if ("0".equals(isFilterKey)) {
			//如果需要过滤
			//List<Keyword> list=getKeywordList();
			return filterKeyword(smsInfo,type,operators,channelCode);
		} else {
			return true;
		}
	}
	private boolean filterKeyword(SmsInfo smsInfo,String type,String operators,String channelCode) {
		//判断全局黑名单
		//boolean boo=false;
		if("1".equals(type)){
			return validateGlobalKeyword(smsInfo);
		}else if("2".equals(type)){
			return validateOperatorsMapKeyword(smsInfo,operators);
		}else if("3".equals(type)){
			return validateChannelMapKeyword(smsInfo,channelCode);
		}else if("4".equals(type)){
			return validateUserMapKeyword(smsInfo);
		}else{
			return true;
		}
	}


	private boolean validateUserMapKeyword(SmsInfo smsInfo) {
		//判断用户黑名单
		userMapKeyword=CacheMap.userMapKeyword;
		User user=smsInfo.getOuser();
		if(userMapKeyword!=null && userMapKeyword.size()>0){
			List<Keyword> userKeyword= userMapKeyword.get(user.getUserNumbers());
			if(userKeyword!=null && userKeyword.size()>0){
				for(Keyword keyword:userKeyword){
					if(smsInfo.getMsg().indexOf(keyword.getMessage())>-1){
						logger.info("mobkey:"+smsInfo.getUser()+"\t"+keyword.getMessage()+"\t"+smsInfo.getMsg()+"\t"+smsInfo.getDest());
						return false;
					}
				}
			}
		}
		return true;
	}
	private boolean validateChannelMapKeyword(SmsInfo smsInfo,String channelCode) {
		//判断通道黑名单
		channelMapKeyword=CacheMap.channelMapKeyword;
		if(channelMapKeyword!=null && channelMapKeyword.size()>0){
			return vaildateChannel(smsInfo,channelCode);
		}
		return true;
	}
	private boolean vaildateChannel(SmsInfo smsInfo, String channelCode) {
		List<Keyword> channelKeyword=channelMapKeyword.get(channelCode);
		if(channelKeyword!=null && channelKeyword.size()>0){
			for(Keyword keyword:channelKeyword){
				if(smsInfo.getMsg().indexOf(keyword.getMessage())>-1){
					logger.info("mobkey:"+smsInfo.getUser()+"\t"+keyword.getMessage()+"\t"+smsInfo.getMsg()+"\t"+smsInfo.getDest()+"\t"+channelCode);
					return false;
				}
			}
		}
		return true;
	}
	private boolean validateOperatorsMapKeyword(SmsInfo smsInfo,String operators) {
		//判断运营商黑名单
		operatorsMapKeyword=CacheMap.operatorsMapKeyword;
		if(operatorsMapKeyword!=null && operatorsMapKeyword.size()>0){
			return validateOperators(smsInfo,operators);
		}
		return true;
	}
	private boolean validateOperators(SmsInfo smsInfo,String operators) {
		List<Keyword> operatorsList=operatorsMapKeyword.get(operators);
		if(operatorsList!=null && operatorsList.size()>0){
			for(Keyword keyword:operatorsList){
				if(smsInfo.getMsg().indexOf(keyword.getMessage())>-1){
					logger.info("mobkey:"+smsInfo.getUser()+"\t"+keyword.getMessage()+"\t"+smsInfo.getMsg()+"\t"+smsInfo.getDest()+"\t"+operators);
					return false;
				}
			}
		}
		return true;
	}
	private boolean validateGlobalKeyword(SmsInfo smsInfo) {
		globalKeyword=CacheMap.globalKeyword;
		if(globalKeyword!=null && globalKeyword.size()>0){
			for(Keyword keyword:globalKeyword){
				if(smsInfo.getMsg().indexOf(keyword.getMessage())>-1){
					logger.info("mobkey:"+smsInfo.getUser()+"\t"+keyword.getMessage()+"\t"+smsInfo.getMsg()+"\t"+smsInfo.getDest());
					return false;
				}
			}
		}
		return true;
	}
}
